import CommonService from '@/api/common';
import { formatTreeData2 } from '@/utils/util';
import _ from 'lodash';
import { ref } from 'vue';
interface Datum {
  childes: Childe2[];
  code: string;
  id: number;
  isLeaf: number;
  level: number;
  name: string;
  parentCode: string;
  parentId: number;
  shortName: string;
  sort: number;
  storey: number;
}

interface Childe2 {
  childes: Childe2[] | [];
  code: string;
  id: number;
  isLeaf: number;
  level: number;
  name: string;
  parentCode: string;
  parentId: number;
  shortName: string;
  sort: number;
  storey: number;
}

// 获取区域列表
export const useProvinceCityList = () => {
  const list = ref<any[]>([]);
  async function get() {
    const resp = await CommonService.selectAreaListFromRedis();
    if (resp.code === 200) {
      list.value = _.get(resp, 'data', []);
    }
  }
  return {
    provinceCityList: list,
    getProvinceCityList: get,
  };
};

// 获取所有行业
export function useAllTreeIndustrys() {
  const list = ref<Datum[]>([]);
  async function get() {
    const resp = await CommonService.selectIndustryListFromRedis<Datum[]>();
    if (resp.code === 200) {
      const v = resp.data!;
      list.value = formatTreeData2(v);
    }
  }

  return {
    industryList: list,
    getIndustryList: get,
  };
}

// 获取岗位
export function useAllTreePositions() {
  const list = ref<Datum[]>([]);
  async function get() {
    const resp = await CommonService.selectPositionListFromRedis<Datum[]>();
    if (resp.code === 200) {
      const v = resp.data!;
      list.value = formatTreeData2(v);
    }
  }

  return {
    positionList: list,
    getPositionList: get,
  };
}

// 获取学历列表
export function useDegreeList(key: string) {
  const list = ref<Datum[]>([]);
  async function get() {
    const resp = await CommonService.selectPositionListFromRedis<Datum[]>();
    if (resp.code === 200) {
      const v = resp.data!;
      list.value = formatTreeData2(v);
    }
  }

  return {
    degreeList: list,
    getDegreeList: get,
  };
}
